package com.ggh.handler;
import com.ggh.entity.RoomBeanP;
import com.ggh.entity.TypeBean;
import com.ggh.entity.UserBean;
import com.ggh.util.ApplicationContextProvider;
import com.ggh.util.JsonUtils;
import com.ggh.websocket.PK_WebSocket_P;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@Slf4j
public class RobBanker_TimePSiRen extends Thread {
	private Map<String, Object> map = new HashMap<>();
	/**
	 * json转换类
	 */
	Gson gson = JsonUtils.get();
	/**
	 * 房间类
	 */
	private RoomBeanP room;
	/**
	 * 时间
	 */
	private int timer;
	/**
	 * 房间操作类
	 */
	private RoomProcess cz=new RoomProcess();
	/**
	* @description : 倒计时
	* @param room
	* @return: null
	* @date : 2019/10/8 11:30
	* @author : jj
	* @throws
	*/
	public RobBanker_TimePSiRen(RoomBeanP room) {
		this.room = room;
	}
	/**
	* @description :房间主运行逻辑
	* @return: void
	* @date : 2019/10/8 11:21
	* @author : jj
	* @throws
	*/
	@Override
	public void run() {
		while (true) {
			/*在此状态下未睡眠线程*/
			if (room == null || room.getState() != 1 && room.getState() != 4 && room.getState() != 6
					&& room.getState() != 7 && room.getState() != 8 && room.getState() != 5 && room.getState() != 3
					&&room.getState()!=9&&room.getState()!=11&&room.getState()!=12&&room.getState()!=13) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				continue;
			}
			/** @description :如果房间退出或者有问题*/
			if (room.getExit_game() == 1 || room.getExit_game() == 2) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				continue;
			}
			timer = room.getTime();
			System.out.println("------------------------时间timer：" + timer);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			timer--;
			System.out.println("time：" + timer);
			/**
			 * 时间存入房间
			 */
			room.setTime(timer);
			try {
				/** @description :抢庄状态时，判断用户的选择情况*/
				if (room.getState() == 1 && timer > 0 && room.getExit_game() != 1) {
					timer=cz.qzdjs(room,timer);
				}
				/**
				 * 准备倒计时结束
				 *  第一局游戏开始，修改对局数
				 */
				if (timer == 0 && room.getState() == 1 && room.getExit_game() != 1) {
					map.clear();
					/* 修改状态
					 * 添加对局*/
					cz.qzdjsjs(room,map);
				}
				/**
				 * 抢庄结束，开启动定庄
				 */
				if (timer == 0 && room.getState() == 3 && room.getExit_game() != 1) {
					map.clear();
					int ksdz = cz.ksdz(room, timer, map);
					timer=ksdz;
				}
				// 下注状态时，判断用户的选择情况
				if (room.getState() == 4 && timer > 0 && room.getExit_game() != 1) {
					map.clear();
					timer=cz.ksxz(room, timer);
				}
				// 下注结束
				if (timer == 0 && room.getState() == 4 && room.getExit_game() != 1) {
					map.clear();
					System.out.println("下注结束-------------");
					// 发完牌，修改房间状态为5，开启
					int xzjs = cz.xzjs(room, map);
					//添加时间
					timer=xzjs;
				}
				/*摇色子*/
				if (timer >0 && room.getState() == 13 && room.getExit_game() != 1){
					timer  = cz.ksysz(room, timer);
				}
				//发牌
				if (timer == 0 && room.getState() == 13 && room.getExit_game() != 1) {
					map.clear();
					int ksfp = cz.ksfp(room, map, timer);
					//修改时间
					timer = ksfp;
				}
				//比牌
				if (room.getState() == 6 && timer == 0 && room.getGame_type() > 4 && room.getExit_game() != 1) {
					map.clear();
					cz.jxbp(room, map);
				}
				/*
				 * 结算
				 * */
				if (room.getState() == 7 && timer == 0) {
					map.clear();
					int jxjs = cz.jxjs(room, map, timer);
					timer = jxjs;
				}
				/**系统提示和个人组牌
				 *组牌状态时，判断用户的选择情况
				 * */
				if (room.getState() == 11 && timer > 0) {
					map.clear();
					int grzports = cz.grzports(room, map, timer);
					timer=grzports;
				}
				// 组牌结束，进行比牌结算结束
				if (timer == 0 && room.getState() == 11 && room.getGame_type() < 5 && room.getGame_type() > 2) {
					map.clear();
					map.put("type", TypeBean.SiRenGameInciDent);
					map.put("messages", gson.toJson(room));
					sendMessageAll(map,room);
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					room.setState(12);
					map.put("type", TypeBean.SiRenGameInciDent);
					map.put("messages", gson.toJson(room));
					sendMessageAll(map,room);
					//结算
					int jxjs=cz.jxjs(room, map, timer);
					timer=jxjs;
				}
				/*
				 * 动漫完结算
				 * */
				if (room.getState() == 8&&timer >0){
					System.out.println(timer);
					// 房间人数
					int a = 0;
					// 动画完结人数
					int b = 0;
					for (int i = 0; i < room.getUser_positions().length; i++) {
						if (room.getUser_positions()[i]==-1){
							continue;
						}
						UserBean ux = room.getUserBean(room.getUser_positions()[i]);
						if (ux.getReady_state() == 4) {
							continue;
						}
						a++;
						if (ux.getIdentify()==1){
							b++;
						}
						/*房间内所有人已对抢庄做出选择，提前结束抢庄*/
						if (b== 1) {
							room.setTime(0);
							timer = 0;
							System.out.println("房间时间" + room.getTime());
						}
					}
				}
				/*
				 * 完结消息存档
				 * */
				if (room.getState()==8&&timer==0){
					map.clear();
					cz.archive(room,map);
					room.setState(9);
					room.setTime(5);
					timer=room.getTime();
				}
				//金币结算时间
				if (room.getState()==9&&room.getTime()>0){
					System.out.println(timer);
					// 房间人数
					int a = 0;
					// 完结人数
					int b = 0;
					for (int i = 0; i < room.getUser_positions().length; i++) {
						if (room.getUser_positions()[i]==-1){
							continue;
						}
						UserBean ux = room.getUserBean(room.getUser_positions()[i]);
						if (ux.getReady_state() == 4) {
							continue;
						}
						a++;
						if (ux.getSettlement()==1){
							b++;
						}
						// 房间内所有人已对抢庄做出选择，提前结束抢庄
						if (b >= 1) {
							room.setTime(0);
							timer = 0;
							System.out.println("房间时间" + room.getTime());
						}
					}
				}
				//开始准备或者退出
				if (room.getState()==9&&timer==0) {
					map.clear();
					if (room.getGame_number()%2==0){
						for (int i = 0; i < room.getUser_positions().length; i++) {
							if (room.getUser_positions()[i]==-1){
								continue;
							}
							UserBean userBean = room.getUserBean(room.getUser_positions()[i]);
							//判断是否有掉线
							if (userBean.getReady_state()==2){
								room.getGame_userlist().remove(userBean);
							}
							/*修改状态*/
							if (userBean!=null) {
								room.getUser_positions()[i]=-1;
								userBean.setLog_lat(-1);
								// 起来成功，修改用户闲家改为--游客
								userBean.setBanker(0);
								// 用户未准备
								userBean.setReady_state(0);
								// 起来,结束循环
								continue;
							}

						}
						room.getUpbanker().clear();
						room.Initialization();
						room.setState(0);
						map.put("type", TypeBean.SiRenGameInciDent);
						map.put("messages", gson.toJson(room));
						sendMessageAll(map,room);
					}else {
						room.setGame_number(room.getGame_number()+1);
						// 所有用户添加对局数
						for (int n = 0; n< room.getGame_userlist().size(); n++) {
							UserBean u = room.getGame_userlist().get(n);
							u.Initialization();
							if (u.getLog_lat() != -1 && u.getReady_state() == 4) {
								u.setReady_state(1);
							} else if (u.getLog_lat() == -1 && u.getReady_state() == 4) {
								u.setReady_state(0);
							}
						}
						//房间初始化
						room.Initialization();
						room.setState(1);
						room.setTime(room.getTime01());
						timer=room.getTime();
						map.put("type", TypeBean.SiRenGameInciDent);
						map.put("messages", gson.toJson(room));
						sendMessageAll(map,room);
					}
				}
			}catch (Exception e){
				System.err.println(e.getMessage());
				log.info("错误日志："+e.getMessage());
				room.setState(0);
			}

		}
	}
	/**
	 * 发送消息给所有人
	 *
	 * @throws IOException
	 */
	public synchronized void sendMessageAll (Map < String, Object > returnMap, RoomBeanP rb){
		for (UserBean userb : rb.getGame_userlist()) {
			PK_WebSocket_P webSocket = Public_State_P.clients.get(String.valueOf(userb.getUserid()));
			if (webSocket != null && webSocket.getSession().isOpen() && userb.getExit_state() == 0) {
				// 不等于自己的则发送消息
				webSocket.sendMessageTo(returnMap, userb);
			} else {
				continue;
			}
		}
	}
	public synchronized void sendMessageTo (Map < String, Object > returnMap,UserBean user ){
		PK_WebSocket_P webSocket = Public_State_P.clients.get(String.valueOf(user.getUserid()));
		if (webSocket != null && webSocket.getSession().isOpen() && user.getExit_state() == 0) {
			// 不等于自己的则发送消息
			webSocket.sendMessageTo(returnMap, user);
		}
	}
}
